<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Initialize View Helpers
     * @return void
     */
    protected function _initViewHelpers()
    {
        $view = new Zend_View();
        $view->addHelperPath(APPLICATION_PATH . '/../library/CC/View/Helper/', 'CC_View_Helper');
        
        Zend_Controller_Action_HelperBroker::addHelper(new CC_Controller_Action_Helper_UserSession());
        
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($view);
        
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
        
        $view->addHelperPath(APPLICATION_PATH . '/../library/ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');
        $view->jQuery()->addStylesheet('/css/flick/jquery-ui.css')->setLocalPath('/js/jquery.min.js')->setUiLocalPath('/js/jquery-ui.min.js');
    }

    /**
     * Initialize auto loaders
     * @return void
     */
    protected function _initAutoloader()
    {
        require_once APPLICATION_PATH . '/../library/Doctrine/Common/ClassLoader.php';
        
        $autoloader = \Zend_Loader_Autoloader::getInstance();
        
        $bisnaAutoloader = new \Doctrine\Common\ClassLoader('Bisna');
        $autoloader->pushAutoloader(array($bisnaAutoloader, 'loadClass'), 'Bisna');
        
        $appAutoloader = new \Doctrine\Common\ClassLoader('NOLASnowball');
        $autoloader->pushAutoloader(array($appAutoloader, 'loadClass'), 'NOLASnowball');
        
        $ccAutoloader = new \Doctrine\Common\ClassLoader('CC');
        $autoloader->pushAutoloader(array($ccAutoloader, 'loadClass'), 'CC');
    }
    
    /**
     * Store Zend Config in registry
     * @return void
     */
    protected function _initConfig()
    {
        $config = new Zend_Config($this->getOptions());
        Zend_Registry::set('config', $config);
        return $config;
    }

    /**
     * Initialize ZFDebug
     * @return void
     */
    protected function _initZFDebug()
    {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('ZFDebug');
        
        $options = array(
        	'plugins' => array(
        		'Auth', 
        		'Variables', 
        		'File' => array('base_path' => '/var/www/code-complex/'), 
        		'Html', 
        		'Memory', 
        		'Time', 
        		'Exception'
            )
        );
        
        # Instantiate the database adapter and setup the plugin.
        # Alternatively just add the plugin like above and rely on the autodiscovery feature.
        if($this->hasPluginResource('db'))
        {
            $this->bootstrap('db');
            $db = $this->getPluginResource('db')->getDbAdapter();
            $options['plugins']['Database']['adapter'] = $db;
        }
        
        # Setup the cache plugin
        if($this->hasPluginResource('cache'))
        {
            $this->bootstrap('cache');
            $cache = $this - getPluginResource('cache')->getDbAdapter();
            $options['plugins']['Cache']['backend'] = $cache->getBackend();
        }
        
        $debug = new ZFDebug_Controller_Plugin_Debug($options);
        
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $frontController->registerPlugin($debug);
    }

    /**
     * Initialize the Flag and Flipper System
     * @return void
     */
    protected function _initFlagFlippers()
    {
        $this->bootstrap('doctrine');
        CC_FlagFlippers_Manager::load();
    }
}

